<?php
class Orderoutdetail extends AdminController {
    public function __construct() {
        parent::__construct();
    }
    
    public function index() {
        $this->_data["loadPage"]="orderoutdetail/index_view";
        $this->_data["titlePage"]="Chi tiết phiếu xuất";
        $this->_data["mess_succ"]=$this->session->flashdata("flash_mess");
        
        $this->load->model("morderoutdetail");
        $oo_id=$this->uri->segment(4);
        $this->_data["oo_id"]=$oo_id;
        if($this->morderoutdetail->listAll($oo_id) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->morderoutdetail->listAll($oo_id);
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function edit() {
        $this->_data["loadPage"]="orderoutdetail/edit_view";
        $this->_data["titlePage"]="Chỉnh sửa chi tiết phiếu xuất";
        
        //load data
        $oo_id=$this->uri->segment(4);
        $this->_data["oo_id"]=$oo_id;
        $ood_id=$this->uri->segment(5);
        $this->load->model("morderout");
        $this->load->model("morderoutdetail");
        $temp=$this->morderoutdetail->getById($ood_id);
        if($temp!=false)
            $this->_data["info_edit"]=$temp;
        
        //validate
        $this->load->helper("form");
        $this->load->library("form_validation");
        $this->form_validation->CI =& $this;
        
        $config=array(
            array(
                "field"=>"txtQuan",
                "label"=>"Số lượng",
                "rules"=>""
            )
        );
        $this->form_validation->set_rules($config);
        
        //update
        if($this->form_validation->run()) {
            $data=array(
                "ood_quantity"=>$this->input->post("txtQuan")
            );
            $this->morderoutdetail->update($data, $ood_id);
            $details=$this->morderoutdetail->listAll($oo_id);
            $total=0;
            foreach($details as $item) {
                $total += $item["ood_price"] * $item["ood_quantity"];
            }
            $data02=array(
                "oo_price"=>$total
            );
            $this->morderout->update($data02, $oo_id);
            $this->session->set_flashdata("flash_mess", "Chỉnh sửa chi tiết phiếu xuất thành công.");
            redirect(base_url()."admin_combo/orderoutdetail/index/$oo_id");
        }
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function del() {
        $id=$this->uri->segment(4);
        $this->load->model("morderoutdetail");
        $this->morderoutdetail->del($id);
        $this->session->set_flashdata("flash_mess", "Xóa chi tiết phiếu xuất thành công.");
        redirect(base_url()."admin_combo/morderoutdetail/");
    }
}
?>